//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite
{
    [Serializable]
    public struct BoundingRectangle : IEquatable<BoundingRectangle>
    {
        public Vector2 Min;
        public Vector2 Max;

        public static readonly BoundingRectangle Empty = 
            new BoundingRectangle(
                new Vector2(float.NaN, float.NaN),
                new Vector2(float.NaN, float.NaN));

        public BoundingRectangle(Vector2 minimum, Vector2 maximum)
        {
            Min = minimum;
            Max = maximum;
        }

        public BoundingRectangle(float left, float bottom, float right, float top)
        {
            Min = new Vector2(left, bottom);
            Max = new Vector2(right, top);
        }

        public static BoundingRectangle CreateFromPoints(IEnumerable<Vector2> points)
        {
            if (points == null)
                throw new ArgumentNullException("points");

            bool hasPoints = false;

            Vector2 min = new Vector2(float.MaxValue);
            Vector2 max = new Vector2(float.MinValue);

            foreach (Vector2 point in points)
            {
                min = Vector2.Min(min, point);
                max = Vector2.Max(max, point);
                hasPoints = true;
            }

            if (!hasPoints)
                throw new ArgumentException("points is empty.");

            return new BoundingRectangle(min, max);
        }

        public ContainmentType Contains(BoundingRectangle rectangle)
        {
            if (Max.X < rectangle.Min.X || Min.X > rectangle.Max.X)
                return ContainmentType.Disjoint;

            if (Max.Y < rectangle.Min.Y || Min.Y > rectangle.Max.Y)
                return ContainmentType.Disjoint;

            if (Min.X <= rectangle.Min.X
                && rectangle.Max.X <= Max.X
                && Min.Y <= rectangle.Min.Y
                && rectangle.Max.Y <= Max.Y)
            {
                return ContainmentType.Contains;
            }

            return ContainmentType.Intersects;
        }

        public ContainmentType Contains(Vector2 point)
        {
            if (Min.X <= point.X 
                && point.X <= Max.X 
                && Min.Y <= point.Y 
                && point.Y <= Max.Y)
            {
                return ContainmentType.Contains;
            }

            return ContainmentType.Disjoint;
        }

        public bool Intersects(BoundingRectangle other)
        {
            return
                Min.X <= other.Max.X 
                && other.Min.X <= Max.X 
                && Min.Y <= other.Max.Y 
                && other.Min.Y <= Max.Y;
        }

        public bool Equals(BoundingRectangle other)
        {
            return Min == other.Min && Max == other.Max;
        }

        public override bool Equals(object obj)
        {
            if (obj is BoundingRectangle)
                return Equals((BoundingRectangle)obj);

            return false;
        }

        public override int GetHashCode()
        {
            return Min.GetHashCode() + Max.GetHashCode();
        }

        public static bool operator ==(BoundingRectangle left, BoundingRectangle right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(BoundingRectangle left, BoundingRectangle right)
        {
            return left.Min != right.Min || left.Max != right.Max;
        }
    }
}
